<?php
/**
 * V7 checked 1
 * 
 * @author Leon
 */
class data_recommend_article extends data_article {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * Event High 执行
	 *	检查friends ids是否合法
	 *	检查是否已经推荐过了
	 *	写入到recommend article表中
	 *	增加friend表的相互推荐的计数
	 * 
	 * @param type $uid
	 * @param type $article_id
	 * @param int $url_id
	 * @param type $friends_str
	 * @return boolean
	 */
	public function add_by_event_on_save_article ($uid, $article_id, $url_id, $friends_str) {
		$uid			= intval($uid);
		$article_id		= intval($article_id);
		$url_id		= intval($url_id);
		$data_friend	= new data_friend();
		$friends_str		= $data_friend->filter_is_friends($uid, $friends_str);	// return id string
		
		if ($article_id and $friends_str) {
			$friends_array	= explode(',', $friends_str);
			$friends_str		= '';						// 清空，下面重新整理
			foreach ($friends_array as $fid) {
				$insert_id = $this->db->execute('
					insert ignore into
						recommend_article
					(sender_id, receiver_id, article_id, url_id, is_read, dt)
						values
					("'.$uid.'", "'.$fid.'", "'.$article_id.'", "'.$url_id.'", 0, "'.  date('Y-m-d H:i:s').'")
				');
				if ($insert_id) {
					$friends_str .= $fid . ',';				// 重新记录写入成功的好友id
				}
			}
			if ($friends_str) {
				$friends_str = substr($friends_str, 0, -1);		// 去掉最后一个“,”
				// 增加好友之间的recommend统计数
				$this->db->execute('
					update
						friend
					set
						total_recommends_send = total_recommends_send + 1
					where
						user_id	= "'.$uid.'"			and
						friend_id	IN ('.$friends_str.')
				');
				$this->db->execute('
					update
						friend
					set
						total_recommends_receive = total_recommends_receive + 1
					where
						friend_id	= "'.$uid.'"			and
						user_id	IN ('.$friends_str.')
				');
				
				return $friends_str;		// 返回检查过的好友，给发布社交信息的时候使用
			}
			return true;
		} else return false;
	}
	/**
	 * 列出recommend
	 *
	 * @param int $uid
	 * @param string $is_read		// all / unread / is_read
	 * @param int $friend_id		// 好友的id
	 * @param int $page
	 * @param int $limit			// 可以为0
	 * @return array
	 */
	public function ls ($uid, $is_read='all/unread/is_read', $friend_id=0, $page=1, $limit=10) {
		$uid			= intval($uid);
		$is_read		= ($is_read === 'unread' or $is_read === 'is_read') ? $is_read : 'all';
		$friend_id		= intval($friend_id);
		$page		= ($page > 1) ? intval($page) : 1;
		$limit			= intval($limit);
		if ($uid) {
			$execute = $this->db->execute('
				select
					recommend_article.id			AS id,
					recommend_article.sender_id		AS sender_id,
					recommend_article.receiver_id	AS receiver_id,
					recommend_article.dt			AS dt,
					save_article.id				AS article_id,
					save_article.title				AS article_title,
					save_article.summary			AS summary,
					save_article.img_0				AS img_0,
					save_article.img_1				AS img_1,
					save_article.img_2				AS img_2,
					article_url.url				AS url
				from
					recommend_article, save_article, article_url
				where
					recommend_article.receiver_id	= "'.$uid.'"						and
					'.(($is_read == 'all') ? ' 1 ' : (($is_read == 'is_read') ? ' recommend_article.is_read = 1 ' : ' recommend_article.is_read = 0 ')).' and
					'.(($friend_id) ? ' recommend_article.sender_id = "'.$friend_id.'"  ' :  ' 1 ' ).'	and
					save_article.id				= recommend_article.article_id		and
					article_url.id				= save_article.url_id
				order by
					recommend_article.id DESC
				'.(($limit) ? ' limit '.(($page-1)*$limit).', '.$limit : '').'
			');
			if (is_array($execute)) {
				$data_user = new data_user();
				foreach ($execute as $k=>$v) {
					$execute[$k]['friend_nickname'] = $data_user->friend_to_nickname($uid, $v['sender_id']);
					$execute[$k]['circles'] = $this->db->execute('
						select
							circle.id,
							circle.title
						from
							circle_article, circle
						where
							circle_article.article_id		= "'.$v['article_id'].'"		and
							circle.id				= circle_article.circle_id		and
							(
								circle.privacy = "public"		or
								circle.id IN ( select circle_id from circle_member where user_id = "'.$uid.'")
							)
						order by
							circle.total_article DESC
					');
				}
				return $execute;
			} else return false;
		} else return false;
	}
	public function del ($uid, $ids) {
		$uid	= intval($uid);
		$ids	= format_id_string($ids, false);		// 返回字符串
		
		if ($uid and $ids) {
			// 查询涉及到的好友列表，为了重新统计推荐数
			$execute = $this->db->execute('
				select
					sender_id
				from
					recommend_article
				where
					id			IN ('.$ids.')		and
					receiver_id		= "'.$uid.'"
				group by
					sender_id
			');
			if (is_array($execute)) {
				// 删除
				$del = $this->db->execute('
					delete from
						recommend_article
					where
						id	IN ('.$ids.')
				');
				if ($del) {
					// 重新统计推荐数
					foreach ($execute as $v) {
						$this->count_recommends($v['sender_id'], $uid);
					}
					return true;
				} else return false;
			} else return false;
		} else return false;
	}
	public function count_recommends ($sender_id, $receiver_id) {
		$sender_id		= intval($sender_id);
		$receiver_id		= intval($receiver_id);
		
		if ($sender_id and $receiver_id) {
			$execute = $this->db->execute('
				select
					COUNT(*) as total
				from
					recommend_article
				where
					sender_id	= "'.$sender_id.'"		and
					receiver_id	= "'.$receiver_id.'"
			');
			$total = intval($execute[0]['total']);
			
			$this->db->execute('
				update
					friend
				set
					total_recommends_send	= "'.$total.'"
				where
					user_id	= "'.$sender_id.'"	and
					friend_id	= "'.$receiver_id.'"
			');
			$this->db->execute('
				update
					friend
				set
					total_recommends_receive	= "'.$total.'"
				where
					user_id	= "'.$receiver_id.'"	and
					friend_id	= "'.$sender_id.'"
			');
			return true;
		} else return false;
	}
	public function event_del_by_save_article ($article_ids) {
		// 查询发送者和接收者，用于重新统计数据
		$execute = $this->db->execute('
			select
				sender_id,
				receiver_id
			from
				recommend_article
			where
				article_id IN ('.$article_ids.')
		');
		if (is_array($execute)) {
			$del = $this->db->execute('
				delete from
					recommend_article
				where
					article_id IN ('.$article_ids.')
			');
			if ($del) {
				foreach ($execute as $v) {
					$this->count_recommends($v['sender_id'], $v['receiver_id']);
				}
				return true;
			} else return false;
		} else return false;
	}
	public function one ($uid, $id) {
		$uid	= intval($uid);
		$id	= intval($id);
		if ($uid and $id) {
			$article = $this->db->execute('
				select
					recommend_article.id	AS id,
					save_article.id		AS article_id,
					save_article.id		AS content_id,
					save_article.user_id	AS user_id,
					save_article.title		AS title,
					save_article.dt		AS dt,
					article_url.id		AS url_id,
					article_url.url		AS url
				from
					recommend_article, save_article, article_url
				where
					recommend_article.id			= "'.$id.'"					and
					recommend_article.receiver_id	= "'.$uid.'"					and
					save_article.id				= recommend_article.article_id 	and
					article_url.id				= save_article.url_id
			');
			if ($article) {
				return $article[0];
			} else return false;
		} else return false;
	}
	public function mark_is_read ($uid, $id) {
		$uid	= intval($uid);
		$id	= intval($id);
		
		if ($uid and $id) {
			return $this->db->execute('
				update
					recommend_article
				set
					is_read	= 1
				where
					id 		= "'.$id.'"		and
					receiver_id	= "'.$uid.'"
			');
		} else return false;
	}
}
?>